#coding=utf-8
'''
Created on 2013年8月30日

@author: xuben

角色列表
'''

from ..common import Command
from ..common.SocketService import gameSocketService
import RoleData

'''
获取等级最高的角色id
'''    
def getMaxLvPlayerId(playerList):
    player = max(playerList, key=lambda p:p['playerLv'])
    return player['playerId']
#     maxPlayerId = 0
#     maxPlayerLv = 0
#     for player in playerList:
#         if player['playerLv'] > maxPlayerLv:
#             maxPlayerId = player['playerId']
#             maxPlayerLv = player['playerLv']
#     return maxPlayerId

'''
获取角色列表
'''
def getRoleList():
    command = Command.GET_ROLE_LIST
    params = ''
    gameSocketService.send(command, params, getRoleListCallback)
    return gameSocketService.receive()

'''
获取角色列表的回调函数
'''
def getRoleListCallback(data):
    if 'playerList' in data:
        RoleData.playerId = getMaxLvPlayerId(data['playerList'])

'''
获取角色信息
'''
def getRoleInfo():
    command = Command.GET_ROLE_INFO
    params = 'playerId=' + str(RoleData.playerId)
    gameSocketService.send(command, params, getRoleInfoCallback)
    return gameSocketService.receive()

'''
获取角色信息的回调函数
'''
def getRoleInfoCallback(data):
    RoleData.init(data)
    
'''
角色数据更新
'''
def roleInfoUpdate(data):
    RoleData.update(data)
    
'''
角色资源更新
'''
def resourceUpdate(data):
    RoleData.updateResources(data)